<style>
.pure_table_extra { width: 100%; margin-top: 5px; }
.pure_table_extra th { height: 25px; line-height: 25px; }
.pure_table_extra img { cursor: pointer; margin-right: 2px; }
.pure_table_extra td { vertical-align: middle; }
</style>

<?php
$pg = Utils::get_param('pg', 1);
$data = $orm->get('pb_ecom_tag', 't.name', 'ASC', $pg, SIZE, NULL)->d;
?>

<div class="pure-form">
  <input id="new_name" type="text" placeholder="{New Tag}" onkeypress="if(event.which == 13) add_obj();">
  <button type="submit" class="pure-button pure-button-primary" onclick="add_obj();">Add Tag</button>
</div>
<table class="pure-table pure_table_extra ui-corner-all" cellpadding=0 cellspacing=0>
  <thead>
    <tr>
      <th class="ui-widget-header">Tag</th>
      <th class="ui-widget-header" style="width: 40px;">&nbsp;</th>
    </tr>
  </thead>
  <tbody id="data_body"></tbody>
</table>
<div>&nbsp;</div>

<div id="dialog_change" class="pure-form" style="display: none;" title="Change Name">
  <input type="text" id="name_change" style="width: 100%;"></input>
</div>

<script type="text/javascript">
  /*<![CDATA[*/
  var __data = JSON.parse('<?php echo addslashes(json_encode($data)); ?>');
  var __requrl = '<?php echo Utils::get_requrl($app_data); ?>';
  var __redmd = '<?php echo addslashes($app_data->sys->redmd); ?>';
  var __sys = '<?php echo addslashes($app_data->sys->php_name); ?>';
  var __id = '<?php echo $app_data->param_id; ?>';
  
  $(function(){
    reset();
    render();
  });

  function reset() {
    __data = __data.sort(function(a, b) {
      return a.name.localeCompare(b.name);
    });
  };

  function render() {
    $('#new_name').val('');
    render_data();
  };
  
  function render_data() {
    $('#data_body').empty();
    for (var idx in __data) {
      var itm = __data[idx];
      $('#data_body').append(
          '<tr class="' + ((idx % 2 == 0) ? 'pure-table-even' : 'pure-table-odd') + '">' + 
            '<td>' + itm.name + '</td>' +
            '<td>' +
              '<span style="cursor: pointer; float: left;" class="ui-button-icon-primary ui-icon ui-icon-pencil" onclick="change_name(' + itm.id + ', event);"></span>' +
              '<span style="cursor: pointer; float: left;" class="ui-button-icon-primary ui-icon ui-icon-closethick" onclick="delete_obj(' + itm.id + ');"></span>' +
            '</td>' + 
          '</tr>');
    }
    if (__data.length == 0) {
      $('#data_body').append('<tr class="pure-table-even"><td colspan=2>None</td></tr>');
    }
  };
  
  function delete_obj(id) {
    var req = {
      id: id,
    };
    Ajax.request(
      __requrl + 'delete_obj',
      'req=' + Utils.encodeURI(JSON.stringify(req)),
      function(rslt) {
        Utils.splice_by_id(id, __data);
        render();
      },
      { confirm: 1 }
    );
  }

  function add_obj() {
    var req = {
      name: $("#new_name")[0].value,
    };
    Ajax.request(
      __requrl + 'submit_obj',
      'req=' + Utils.encodeURI(JSON.stringify(req)),
      function(rslt) {
        __data.push(rslt);
        reset();
        render();
      },
      null
    );
  }

  function change_name(id, ev) {
    var func = function() {
      var req = Utils.get_by_id(id, __data);
      req.name = $('#name_change').val();
      Ajax.request(
        __requrl + 'submit_obj',
        'req=' + Utils.encodeURI(JSON.stringify(req)),
        function(rslt) {
          reset();
          render();
          $('#dialog_change').dialog('close');
        },
        null
      );
    };
    
    $('#dialog_change').dialog({
      position: {
        my: 'left+0 bottom+0',
        of: ev.target
      },
      buttons: {
        'OK': function() {
          func();
        },
        'Cancel': function() { 
          $(this).dialog('close');
        }
      }
    });

    var obj = Utils.get_by_id(id, __data);

    $('#name_change')[0].onkeypress = function(ev) {
      if(ev.which == 13) {
        func();
      }
    };
    $('#name_change').val(obj.name);
    $('#name_change').select();
  }
  /*]]>*/
</script>